package com.sinochem.sales.workflow.service.impl;

import com.sinochem.sales.workflow.entity.UserLeave;
import com.sinochem.sales.workflow.service.LeaveService;
import org.apache.log4j.Logger;
import org.sagacity.sqltoy.dao.SqlToyLazyDao;
import org.sagacity.sqltoy.model.PaginationModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xuan😴🐶🐱🐽🌺  <a href="zhangshixuanj@163.com">联系作者</a>
 * @version Revision:v1.0,Date: 2018/5/7 11:06
 * @project parent
 * @description
 * @Modification Date: 2018/5/7 11:06  {填写修改说明}
 */
@Service
public class LeaveServiceImpl implements LeaveService {

    /**
     * 日志
     */
    private static Logger logger = Logger.getLogger(LeaveServiceImpl.class);
    /**
     * 数据库操作
     */
    @Resource
    private SqlToyLazyDao sqlToyLazyDao;


    @Override
    public void saveOrUpdate(UserLeave userLeave) {
        try {
            sqlToyLazyDao.saveOrUpdate(userLeave);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("添加或者保存失败");
        }
    }

    @Override
    public List<UserLeave> findLeavePage(UserLeave userLeave) {
        PaginationModel page = new PaginationModel();
        try {
            PaginationModel find_leave_page = sqlToyLazyDao.findPageBySql(page, "find_leave_page",
                    UserLeave.class);
            return find_leave_page.getRows();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("分页查询失败!");
        }
    }

    @Override
    public UserLeave findLeave(UserLeave userLeave) {
        try {
            return (UserLeave) sqlToyLazyDao.loadBySql("find_leave", new String[]{"id"},
                    new Object[]{userLeave.getId()},
                    UserLeave.class);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("加载信息失败");
        }
    }
}
